package com.woniuxy.carlife.util;

import com.alibaba.fastjson.JSON;
import com.woniuxy.carlife.domain.ServiceInfo;
import com.woniuxy.carlife.dto.ServiceInfoDto;
import lombok.Data;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author fx
 * @ClassName:ServiceInfoUtil
 * @Description: 服务详情把图文详情转换到dto类的工具类
 * @date 2021/4/13 18:12
 **/
@Data
@Component
public class ServiceInfoUtil {

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 功能描述〈传入集合转换〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/13 19:09
     * @throws
     */
    public List<ServiceInfoDto> infoToDtoList(List<ServiceInfo> info){
        ArrayList<ServiceInfoDto> s = new ArrayList<>();
        for (ServiceInfo sr : info) {
            ServiceInfoDto se = new ServiceInfoDto();
            //serviceInfoId主键、自增
            se.setServiceInfoId(sr.getServiceInfoId());
            //对应门店id
            se.setStoreId(sr.getStoreId());
            //对应服务类别id
            se.setServiceCategoryId(sr.getServiceCategoryId());
            //类别名称
            se.setServiceCategoryName(sr.getServiceCategoryName());
            //服务图标
            se.setServiceInfoIcon(sr.getServiceInfoIcon());
            //服务项目名称
            se.setServiceInfoName(sr.getServiceInfoName());
            //服务介绍
            se.setServiceIntroduction(sr.getServiceIntroduction());
            //服务状态
            se.setServiceInfoStatus(sr.getServiceInfoStatus());
            //服务原价
            se.setServiceInfoOriginalPrice(sr.getServiceInfoOriginalPrice());
            //优惠价格
            se.setServiceInfoDiscountPrice(sr.getServiceInfoDiscountPrice());
            //实际售价
            se.setServiceInfoActuallyPrice(sr.getServiceInfoActuallyPrice());
            //平台抽成比率
            se.setRoyaltyRatio(sr.getRoyaltyRatio());
            //收藏量从reids查
            String o = (String) redisTemplate.opsForValue()
                    .get("com.woniuxy:carlife:serviceproject:serviceinfo:id:" + sr.getServiceInfoId());
            if (ObjectUtils.isEmpty(o)){
                //转换成json
                String string = JSON.toJSONString(sr);
                redisTemplate.opsForValue()
                        .set("com.woniuxy:carlife:serviceproject:serviceinfo:id:"+sr.getServiceInfoId(),
                                string);
                se.setServiceCollect(sr.getServiceCollect());
                se.setServiceLike(sr.getServiceLike());
                se.setServiceSaleVolume(sr.getServiceSaleVolume());
            }else{
                ServiceInfo info1 = JSON.parseObject(o, ServiceInfo.class);
                se.setServiceCollect(info1.getServiceCollect());
                //点赞量
                se.setServiceLike(info1.getServiceLike());
                //销售量
                se.setServiceSaleVolume(sr.getServiceSaleVolume());
            }
            //评分
            se.setServiceScore(sr.getServiceScore());
            //图文详情
            List<String> list = JSON.parseObject(sr.getServicePictureInfo(), List.class);
            se.setServicePictureInfo(list);
            //讲对象添加进dto集合中
            s.add(se);
        }
        return s;
    }

    /**
     * 功能描述〈传入单个字符串转换〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/13 19:09
     * @throws
     */
    public ServiceInfoDto infoToDtoOne(ServiceInfo sr){
        ServiceInfoDto se = new ServiceInfoDto();
        //serviceInfoId主键、自增
        se.setServiceInfoId(sr.getServiceInfoId());
        //对应门店id
        se.setStoreId(sr.getStoreId());
        //对应服务类别id
        se.setServiceCategoryId(sr.getServiceCategoryId());
        //类别名称
        se.setServiceCategoryName(sr.getServiceCategoryName());
        //服务图标
        se.setServiceInfoIcon(sr.getServiceInfoIcon());
        //服务项目名称
        se.setServiceInfoName(sr.getServiceInfoName());
        //服务介绍
        se.setServiceIntroduction(sr.getServiceIntroduction());
        //服务状态
        se.setServiceInfoStatus(sr.getServiceInfoStatus());
        //服务原价
        se.setServiceInfoOriginalPrice(sr.getServiceInfoOriginalPrice());
        //优惠价格
        se.setServiceInfoDiscountPrice(sr.getServiceInfoDiscountPrice());
        //实际售价
        se.setServiceInfoActuallyPrice(sr.getServiceInfoActuallyPrice());
        //平台抽成比率
        se.setRoyaltyRatio(sr.getRoyaltyRatio());
        //收藏量从reids查
        String o = (String) redisTemplate.opsForValue()
                .get("com.woniuxy:carlife:serviceproject:serviceinfo:id:" + sr.getServiceInfoId());
        if (ObjectUtils.isEmpty(o)){
            //转换成json
            String string = JSON.toJSONString(sr);
            redisTemplate.opsForValue()
                    .set("com.woniuxy:carlife:serviceproject:serviceinfo:id:"+sr.getServiceInfoId(),
                            string);
            se.setServiceCollect(sr.getServiceCollect());
            se.setServiceLike(sr.getServiceLike());
            se.setServiceSaleVolume(sr.getServiceSaleVolume());
        }else{
            ServiceInfo info1 = JSON.parseObject(o, ServiceInfo.class);
            se.setServiceCollect(info1.getServiceCollect());
            //点赞量
            se.setServiceLike(info1.getServiceLike());
            //销售量
            se.setServiceSaleVolume(sr.getServiceSaleVolume());
        }
        //评分
        se.setServiceScore(sr.getServiceScore());
        //图文详情
        String s=sr.getServicePictureInfo();
        List<String> list = JSON.parseArray(sr.getServicePictureInfo(), String.class);
        se.setServicePictureInfo(list);
        return se;
    }

    /**
     * 功能描述〈判断参数对象是否为空〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/13 20:22
     * @throws
     */
    public static boolean checkObjAllFieldsIsNull(Object object) {
        if (null == object) {
            return true;
        }
        try {
            for (Field f : object.getClass().getDeclaredFields()) {
                f.setAccessible(true);

                if (f.get(object) != null && !StringUtils.isEmpty(f.get(object).toString())) {
                    return false;
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}
